<?php

class AppTemplate
{	
	
	function processRender($name, $url, $context)
	{
		$smarty = new Smarty;	

		$UrlTemplate  = $context->PathView()  . FileUtil::Slash(). $url  .FileUtil::Slash();	
		
		//echo $UrlTemplate;exit;		
		$smarty->template_dir = $UrlTemplate;		
		
		$smarty->compile_dir = $context->SystemRootPath() . FileUtil::Slash(). "cache" .FileUtil::Slash() . "compiler";
		$smarty->cache_dir = $context->SystemRootPath() . FileUtil::Slash(). "cache" .FileUtil::Slash() . "cache";
					
		$smarty->left_delimiter = "{{";
		$smarty->right_delimiter = "}}";	
		
		foreach ($context->getVariavel() as $key => $value)
		{
			$smarty->assign($key, $value);				
		}			

		foreach ($context->getHelper() as $name)
		{		
			include_once($context->PathHelper() . strtolower($name)."_helper.php");				
			$t = new FkEditorHelper($context);
			$classModel = new ReflectionClass($name."helper");			
			$resultModel = $classModel->newInstance($context);			
			$smarty->assign(strtolower($name), $resultModel);					
		}	
					
		$smarty->assign("URLLAYOUT", $context->PathLayout());
		$smarty->assign("WEB", $context->webroot());       
		$smarty->assign("html", new HTML($context));
        
		
		$name = $url.'.'.$name .".html";
		$html = $smarty->fetch($name);
					
		return $html;
	}
	
	function processController($module, $context)
	{
		$smarty = new Smarty;
				
			$arrModule = explode('.', $context->getModule());			
			$path = implode(FileUtil::Slash(), $arrModule);			
			//$moduleToLoad = $context->getDirController() .FileUtil::Slash(). $path . FileUtil::Slash() . $name ."_controller.php";		
			
			array_push($arrModule, $context->getAction());
			$nomeTemplate = implode(".", $arrModule) .".html";		
			$UrlTemplate  = $context->PathView()  . FileUtil::Slash(). $path  .FileUtil::Slash();						
			
			if(!$module->getTemplate())
			{
				$smarty->template_dir = $UrlTemplate;			
			}
			else
			{
				$smarty->template_dir = $context->PathLayout();
				$nomeTemplate = $module->getTemplate();			
			}
			
			$smarty->compile_dir = $context->SystemRootPath() . FileUtil::Slash(). "cache" .FileUtil::Slash() . "compiler";
			$smarty->cache_dir = $context->SystemRootPath() . FileUtil::Slash(). "cache" .FileUtil::Slash() . "cache";
			
			if($context->getConfig("CACHE") || $module->cache)
			{
				$smarty->caching = true;
			}
			
			$smarty->left_delimiter = "{{";
			$smarty->right_delimiter = "}}";
		
			foreach ($context->getVariavel() as $key => $value)
			{
				$smarty->assign($key, $value);				
			}
			
			foreach ($context->getHelper() as $name)
			{		
				include_once($context->PathHelper() . strtolower($name)."_helper.php");				
				$t = new FkEditorHelper($context);
				$classModel = new ReflectionClass($name."helper");			
				$resultModel = $classModel->newInstance($context);			
				$smarty->assign(strtolower($name), $resultModel);					
			}
		
			$context->data = $module->data;
			$smarty->assign('data',$module->data);				
			$smarty->assign("URLLAYOUT", $context->PathLayout());
			$smarty->assign("WEB", $context->webroot());
			$smarty->assign("html", new HTML($context));

            if($context->getConfig('ENABLE_LANGUAGE'))
            {
                $lang = $context->getLanguage();
            }
            else
            {
                $lang ="";
            }
            $smarty->assign("CONFIG",'Config = {web:"'.$context->webroot().'", lang:"'.$lang.'"}');

            
			$html = $smarty->fetch($nomeTemplate);			
			if(!empty($module->layout))
			{
				$smarty->template_dir = $context->PathLayout();
				$smarty->assign("CONTENT", $html);
				$html = $smarty->fetch($module->layout . '_layout.html');			
			}
			
			$context->removeSession('myShowF');
			return $html;
	}
}

?>